<?php

namespace Mlym\CodeGeneration\Middleware\Bean\Module;

use EasySwoole\Spl\SplBean;

class ModuleBean extends SplBean
{
    protected $api;
    protected $name;
    protected $table_name;
    protected $table_prefix;
    protected $alias;
    protected $comment;
    protected $condition;
    protected $schema = [];
    protected $relation = [];
    private $moduleAliasMapping = [];

    protected function initialize(): void
    {
        foreach ($this->schema as &$item) {
            $item = new SchemaColumnBean($item);
        }
        foreach ($this->relation as &$item) {
            $item = new RelationBean($item);
            $this->moduleAliasMapping[$item->getName()] = $item->getAlias() ?? $item->getName();
        }
    }

    protected function setClassMapping(): array
    {
        return [
            'api' => ApiBean::class
        ];
    }


    /**
     * @return mixed
     */
    public function getApi(): ApiBean
    {
        return $this->api;
    }

    /**
     * @param mixed $api
     */
    public function setApi($api): void
    {
        $this->api = $api;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name): void
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getTableName()
    {
        return $this->table_name ?? $this->name;
    }

    /**
     * @param mixed $table_name
     */
    public function setTableName($table_name): void
    {
        $this->table_name = $table_name;
    }

    /**
     * @return mixed
     */
    public function getTablePrefix()
    {
        return $this->table_prefix ?? '';
    }

    /**
     * @param mixed $table_prefix
     */
    public function setTablePrefix($table_prefix): void
    {
        $this->table_prefix = $table_prefix;
    }


    /**
     * @return mixed
     */
    public function getAlias()
    {
        return $this->alias ?? $this->getTableName();
    }

    /**
     * @param mixed $alias
     */
    public function setAlias($alias): void
    {
        $this->alias = $alias;
    }


    /**
     * @return mixed
     */
    public function getComment()
    {
        return $this->comment;
    }

    /**
     * @param mixed $comment
     */
    public function setComment($comment): void
    {
        $this->comment = $comment;
    }


    /**
     * @return mixed
     */
    public function getCondition()
    {
        return $this->condition;
    }

    /**
     * @param mixed $condition
     */
    public function setCondition($condition): void
    {
        $this->condition = $condition;
    }

    /**
     * @return mixed
     */
    public function getSchema(): array
    {
        return $this->schema;
    }

    /**
     * @param mixed $schema
     */
    public function setSchema($schema): void
    {
        $this->schema = $schema;
    }

    /**
     * @return mixed
     */
    public function getRelation(): array
    {
        return $this->relation;
    }

    /**
     * @param mixed $relation
     */
    public function setRelation($relation): void
    {
        $this->relation = $relation;
    }

    public function getRelationAlias($moduleName)
    {
        return $this->moduleAliasMapping[$moduleName] ?? '';
    }


}